package com.cedifie.lendapp.ui.loan.impl

import android.Manifest
import android.content.Context
import com.cedifie.lendapp.analysis.TestDataLibInvoke
import com.cedifie.lendapp.http.IHttpCallback
import com.cedifie.lendapp.mvp.presenter.MvpPresenter
import com.cedifie.lendapp.ui.loan.view.ICedLoanLimitView
import com.cedifie.lendapp.utils.helper.PermissionHelper
import com.cedifie.lendapp.utils.helper.SharedHelper
import com.cedifie.lib_permission.Permission
import com.cedifie.lib_permission.XXPermissions
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/17 13:45
 * @Annotation：贷款额度 Presenter 实现
 */
class CedLoanLimitPresenterImpl(context: Context?) : MvpPresenter<ICedLoanLimitView>(context) {

    private var limitModel: CedLoanLimitModelImpl? = null

    init {
        limitModel = CedLoanLimitModelImpl()
    }

    /**
     * 获取贷款额度
     */
    fun loadCedLoanLimitAction(){
        limitModel?.loadCedLoanLimitAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        //需要显示的金额JSON
                        var amountObject: JSONObject? = null
                        val commonString = SharedHelper.getCedSPString(SharedHelper.CED_SPKEY.CEDIFIE_COMMON)
                        if (commonString.isEmpty()){
                            return
                        }
                        //未完善资料之前，显示额度从启动页配置数据中取
                        val commonData = JSONObject(commonString)
                        val customize = commonData.optJSONObject("customize")
                        if (customize != null){
                            amountObject = customize.optJSONObject("loanLimit")
                        }
                        //后台没有配置loanLimit 默认额度
                        if (amountObject == null){
                            amountObject = JSONObject()
                            amountObject.put("amount1","35000")
                            amountObject.put("amount2","15000")
                            amountObject.put("amount3","20000")
                        }

                        //完善资料之后显示的可申请金额需要从贷款额度接口中取 amount
                        val limitArray = obj?.optJSONArray("limit")
                        if (limitArray != null && limitArray.length() > 0){
                            val limitObj = limitArray.optJSONObject(0)
                            amountObject.put("amount",limitObj.optString("amount"))
                        }
                        mvpView?.showCedLoanLimitData(amountObject)
                    }
                    else -> {
                        mvpView?.showCedToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showCedToast(error.optString("errorMsg"))
            }
            override fun onRequestFinish() {
                mvpView?.hideCedLoading()
            }
        })
    }

    /**
     * 检查权限是否全部申请
     */
    fun checkCedAllPermissionsAgree(){
        mvpView?.getContext()?.let {
            //权限列表未操作 -> 进入权限列表
            val permissionType = SharedHelper.getCedSPInt(SharedHelper.CED_SPKEY.CEDIFIE_PERMISSION_REASON_TYPE)
            if (permissionType == 0){
                mvpView?.showCedPermissionReason()
                return
            }
            //是否是广告渠道
            val isAdSource = SharedHelper.getCedSPBoolean(SharedHelper.CED_SPKEY.CEDIFIE_IS_ADSOURCE)
            //广告渠道 && 权限列表未同意 -> 进入权限列表
            if (isAdSource && permissionType == -1){
                mvpView?.showCedPermissionReason()
                return
            }
            //广告渠道 && 未同意sms权限 -> 进入权限列表
            if (isAdSource && !XXPermissions.isGranted(it, Manifest.permission.READ_SMS)){
                mvpView?.showCedPermissionReason()
                return
            }

            //已同意权限列表 && 已同意SMS权限 -> 上报设备数据
            if (permissionType == 1 && XXPermissions.isGranted(it, Permission.READ_SMS)){
                //上报设备数据
                TestDataLibInvoke.loadCedPhoneInfoListAction()
            }
            mvpView?.showCedPermissionCheckSuccess()
        }
    }
}